<template>
  <div class="pad20">
    <div>
      <el-form :inline="true" size="mini">
        <el-form-item label="广告标题：">
          <el-input v-model.trim="map.cTitle"></el-input>
        </el-form-item>
        <el-form-item label="状态:" >
          <el-select v-model="map.status" class="auto-width" clearable filterable placeholder="状态" style="width: 85px">
            <el-option
              v-for="item in opts.statusIdList"
              :key="item.code"
              :label="item.desc"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon='el-icon-search' type="primary" @click="handleCheck">查询</el-button>
          <el-button icon='el-icon-refresh' class="filter-item" @click="handleReset">重置</el-button>
          <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="handleAddRow()">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table v-loading="ctrl.loading" size="medium" :data="list" stripe border style="width: 100%">
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column  label="广告图片" width="222">
          <template slot-scope="scope">
            <img :src="scope.row.cImage" width="200"/>
          </template>
        </el-table-column>
        <el-table-column prop="cTitle" label="广告标题"></el-table-column>
        <el-table-column prop="startTime" label="开始时间" width="180">
        </el-table-column>
        <el-table-column prop="endTime" label="结束时间" width="180">
        </el-table-column>
        <el-table-column
          label="状态"
          prop="status"
          align="center"
          width="200">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              @change="handleChangeStatus(scope.row.cid, scope.row.status)"
              :active-value="1"
              :inactive-value="0"
              active-color="#ff4949"
              inactive-color="#13ce66"
              active-text="禁用"
              inactive-text="正常">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="170">
          <template slot-scope="scope">
                <el-button type="danger" @click="handleDelRow(scope.row.cid)" size="mini">删除</el-button>
                <el-button type="success" @click="handleUpdateRow(scope.row)" size="mini">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mgt20">
        <el-pagination
          background
          style="float: right;margin-top: 20px; margin-bottom: 22px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.current"
          :page-size="page.size"
          :page-sizes="[2,3,4]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    </div>
    <edit :visible="ctrl.dialogVisible" :formData="formData" :title="ctrl.dialogTitle" @close-callback="closeCallback"></edit>
    <add :visible="ctrl.addDialogVisible" :title="ctrl.dialogTitle" @close-callback="closeCallback"></add>
  </div>
</template>
<script>
  // import * as api from '@/api/homepage'
  import Edit from './edit'
  import Add from './add'
  export default {
    components: { Edit, Add },
    data() {
      return {
        // 页面控制数据，例如形式弹窗，显示加载中等
        ctrl: {
          loading: false,
          remoteAuthorLoading: false,
          dialogVisible: false,
          addDialogVisible: false
        },
        opts: {
          statusIdList: [
            {
              code: '0',
              desc: '正常'
            }, {
              code: '1',
              desc: '禁用'
            }
          ]
        },
        // 条件筛选参数
        map: {
          cTitle: "",
          status: ""
        },
        // 表单数据, 例如新增编辑子项，页面表单
        formData: {},
        list: [],
        page: {
          current: 1,
          size: 2,
          total: 0
        }
      }
    },
    mounted() {
      this.getList()
    },
    methods: {
      //新增
      handleAddRow() {
        this.ctrl.dialogTitle = '新增'
        this.ctrl.addDialogVisible = true
      },
      //编辑
      handleUpdateRow(data) {
        this.formData = data
        this.ctrl.dialogTitle = '编辑'
        this.ctrl.dialogVisible = true
      },
      // 关闭编辑弹窗回调
      closeCallback() {
        this.ctrl.dialogVisible = false;
        this.ctrl.addDialogVisible = false
        this.reload()
      },
      handleChangeStatus(id, statusId) {
        const title = { 0: '启用', 1: '禁用' }
        this.$confirm(`确定要${title[statusId]}吗?`, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.ctrl.loading = true
          this.changeStatus(id, statusId)
        }).catch(() => {
        })
        this.reload()
      },
      //改变状态
      changeStatus(id, statusId) {
        this.$axios.post(`education-system/sys/carousel/advSave?cid=${id}&status=${statusId}`).then(d => {
          this.ctrl.loading = false
          if (d.status == 200 && d.data.obj == true) {
            const msg = { 0: '启用成功', 1: '禁用成功' }
            this.$message({
              type: 'success',
              message: msg[statusId]
            });
            this.reload()
          } else {
            const msg = { 0: '启用失败', 1: '禁用失败' }
            this.$message({
              type: 'error',
              message: msg[statusId]
            });
            this.reload()
          }
        }).catch(() => {
          this.ctrl.loading = false
          this.reload()
        })
      },
      //删除
      handleDelRow(id) {
        this.$confirm(`确定删除这条数据?`, '我要删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.ctrl.loading = true
          this.$axios.get(`education-system/sys/carousel/advDelete/${id}`).then(r=>{
            this.ctrl.loading = false
            if(r.status == 200 && r.data.obj == true){
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              //刷新table
              this.reload();
            }else{
              this.$message({
                type: 'error',
                message: '删除失败!'
              });
            }
          }).catch(() => {
            this.ctrl.loading = false
          })
        }).catch(() => {
        })
      },
      // 刷新页面
      reload() {
        this.map = {}
        this.formData = {}
        this.getList()
      },
      handleSizeChange(val) {
        // console.log(`每页 ${val} 条`)
        this.page.size = val
        this.page.current = 1;
        this.getList()
      },
      handleCurrentChange(val) {
        this.page.current = val
        this.getList()
        // console.log(`当前页: ${val}`)
      },
      // 查询条件
      handleCheck() {
        this.page.current = 1
        this.getList()
      },
      // 重置查询条件
      handleReset() {
        this.page.current = 1;
        this.reload()
      },
      //轮播广告列表
      getList() {
        this.ctrl.loading = true
        this.$axios.get(`education-system/sys/carousel/getAdvByPage?page=${
          this.page.current}&limit=${this.page.size}&cTitle=${
          this.map.cTitle}&status=${
          this.map.status}`).then(d => {
          // console.log(d);
          this.ctrl.loading = false
          this.list = d.data.obj.records;
          this.page.total = d.data.obj.total;
        }).catch(() => {
          this.ctrl.loading = false
        })
      }
    }
  }
</script>
